package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mall.dto.LoginDTO;
import com.yang.mall.dto.MemberDTO;
import com.yang.mall.entity.Member;
import com.yang.mall.entity.QueryRequest;
import com.yang.mall.exception.OLException;
import com.yang.mall.mapper.MemberMapper;
import com.yang.mall.service.IMemberService;
import com.yang.mall.tools.BeanUtils;
import com.yang.mall.tools.JWTUtils;
import com.yang.mall.vo.MemberVO;
import com.yang.mall.vo.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    /**
     * 登陆
     * @param loginDTO
     * @author:kadorlin
     * @date:2022/10/24 11:06
     * @return:com.yang.mall.entity.Member
     **/
    @Override
    public MemberVO login(LoginDTO loginDTO) {
        QueryWrapper<Member> qw  = new QueryWrapper<Member>();
        qw.lambda()
                .eq(Member::getPassword,loginDTO.getPassword())
                .eq(Member::getUsername,loginDTO.getUsername());
        Member member = this.getOne(qw);
        if(Objects.isNull(member)){
            throw new OLException("用户名或密码错误~");
        }
        if(!member.getStatus().equals("1")){
            throw new OLException("用户审核中，请稍后再试~");
        }
        MemberVO memberVO = BeanUtils.copyBeanProp(member, MemberVO.class);
        //432000000 一个月
        String token = JWTUtils.createJWT(member.getMemberId().toString(), 432000000);//5天过期
        memberVO.setToken(token);
        return memberVO;
    }

    /**
     * 注册用户
     * @param member
     * @author:kadorlin
     * @date:2022/10/24 11:15
     * @return:void
     **/
    @Override
    public void register(Member member) {
        QueryWrapper<Member> qw  = new QueryWrapper<Member>();
        qw.lambda()
                .eq(Member::getUsername,member.getUsername());
        Member one = this.getOne(qw);
        if(Objects.nonNull(one)){
            throw new OLException("用户名已被注册～～～");
        }
        member.setStatus("0");
        this.save(member);
    }

    /**
     * 会员列表
     * @param memberDTO
     * @author:kadorlin
     * @date:2022/10/24 13:28
     * @return:com.baomidou.mybatisplus.core.metadata.IPage<com.yang.mall.vo.MemberVO>
     **/
    @Override
    public IPage<MemberVO> findPage(MemberDTO memberDTO) {
        Page<Member> page = new Page<>(memberDTO.getPageNum(), memberDTO.getPageSize());
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(Member::getDelFlag,"0")
                .like(StringUtils.isNotEmpty(memberDTO.getAddress()),Member::getAddress,memberDTO.getAddress())
                .like(StringUtils.isNotEmpty(memberDTO.getMobile()),Member::getMobile,memberDTO.getMobile())
                .like(StringUtils.isNotEmpty(memberDTO.getName()),Member::getName,memberDTO.getName())
                .like(StringUtils.isNotEmpty(memberDTO.getShopName()),Member::getShopName,memberDTO.getShopName());
        IPage<Member> memberIPage = this.page(page, qw);
        return memberIPage.convert(item->BeanUtils.copyBeanProp(item,MemberVO.class));
    }

    /**
     * 审核会员
     * @param memberDTO
     * @author:kadorlin
     * @date:2022/10/24 15:18
     * @return:void
     **/
    @Override
    public void audit(MemberDTO memberDTO) {
        Member member = BeanUtils.copyBeanProp(memberDTO, Member.class);
        this.updateById(member);
    }
}
